package com.mypack.student.service.student.impl;

import com.alibaba.excel.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.mypack.student.constants.EnumDeleteFlag;
import com.mypack.student.constants.EnumResponse;
import com.mypack.student.dto.student.StudentDTO;
import com.mypack.student.dto.student.StudentDeleteDTO;
import com.mypack.student.dto.student.StudentExportDTO;
import com.mypack.student.dto.student.StudentSearchDTO;
import com.mypack.student.entity.EduStudent;
import com.mypack.student.exception.IdCartNoExistException;
import com.mypack.student.mapper.StudentMapper;
import com.mypack.student.service.student.StudentService;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private MapperFacade mapperFacade;

    @Override
    public List<StudentDTO> listStudents(StudentSearchDTO searchDTO, boolean isPage) {

        if (isPage) {
            PageHelper.startPage(searchDTO.getPageNum(), searchDTO.getPageSize());
        }

        return studentMapper.listStudents(searchDTO);
    }

    @Override
    public void addStudent(StudentDTO studentDTO) throws IdCartNoExistException {



        int countNum = studentMapper.countStudentByIdCartNo(studentDTO.getIdentityCardNo(),null);
        if(countNum>0){
            throw new IdCartNoExistException(EnumResponse.ID_CART_NO_EXIST_EXCEPTION);
        }

        studentDTO.setDeleteFlag(EnumDeleteFlag.NO.getCode());
        studentDTO.setCreateTime(new Date());
        studentDTO.setUpdateTime(new Date());

        EduStudent eduStudent = mapperFacade.map(studentDTO, EduStudent.class);

        studentMapper.insert(eduStudent);

    }

    @Override
    public void updateStudent(StudentDTO studentDTO) throws IdCartNoExistException{

        if (studentDTO.getId() == null) {
            return;
        }

        int count = studentMapper.countStudentByIdCartNo(studentDTO.getIdentityCardNo(),studentDTO.getId());
        if(count > 0){
            throw new IdCartNoExistException(EnumResponse.ID_CART_NO_EXIST_EXCEPTION);
        }

        EduStudent eduStudent = mapperFacade.map(studentDTO, EduStudent.class);

        studentMapper.updateById(eduStudent);

    }

    @Override
    public void delete(StudentDeleteDTO studentDeleteDTO) {

        StudentDTO studentDTO = new StudentDTO();
        studentDTO.setId(studentDeleteDTO.getId());
        studentDTO.setRemark(studentDeleteDTO.getRemark());
        studentDTO.setDeleteTime(new Date());
        studentDTO.setDeleteUserId(studentDeleteDTO.getDeleteUserId());
        studentDTO.setDeleteFlag(EnumDeleteFlag.YES.getCode());

        EduStudent eduStudent = mapperFacade.map(studentDTO, EduStudent.class);

        studentMapper.updateById(eduStudent);

    }

    @Override
    public List<StudentExportDTO> exportStudents(StudentSearchDTO searchDTO) {

        List<StudentDTO> studentDTOList = listStudents(searchDTO, false);

        if (CollectionUtils.isEmpty(studentDTOList)) {
            return Lists.newArrayList();
        }

        List<StudentExportDTO> studentExportDTOList = mapperFacade.mapAsList(studentDTOList, StudentExportDTO.class);


        studentExportDTOList.forEach(item -> {
            item.setSexStr(new Integer(1).equals(item.getSex()) ? "男" : "女");
            item.setStartTimeStr(DateUtils.format(item.getStartTime(), DateUtils.DATE_FORMAT_10));
            item.setAllAddress(item.getProvince() + item.getCity() + item.getAddress());
        });

        return studentExportDTOList;
    }
}
